package org.example.springboot.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.example.springboot.DTO.BorrowDTO;
import org.example.springboot.common.Result;
import org.example.springboot.entity.BorrowRecord;
import org.example.springboot.service.BorrowRecordService;
import org.springframework.web.bind.annotation.*;

/**
 * 借阅记录控制器
 */
@Tag(name = "借阅管理接口")
@RestController
@RequestMapping("/borrow")
public class BorrowRecordController {
    @Resource
    private BorrowRecordService borrowRecordService;
    
    @Operation(summary = "借阅图书")
    @PostMapping("/book")
    public Result<?> borrowBook(@RequestParam Long userId, @RequestBody @Valid BorrowDTO borrowDTO) {
        BorrowRecord borrowRecord = borrowRecordService.borrowBook(userId, borrowDTO);
        return Result.success(borrowRecord);
    }
    
    @Operation(summary = "归还图书")
    @PostMapping("/return")
    public Result<?> returnBook(@RequestParam Long userId, @RequestBody BorrowDTO borrowDTO) {
        BorrowRecord borrowRecord = borrowRecordService.returnBook(userId, borrowDTO.getId());
        return Result.success(borrowRecord);
    }
    
    @Operation(summary = "续借图书")
    @PostMapping("/renew")
    public Result<?> renewBook(@RequestParam Long userId, @RequestBody BorrowDTO borrowDTO) {
        BorrowRecord borrowRecord = borrowRecordService.renewBook(userId, borrowDTO.getId());
        return Result.success(borrowRecord);
    }

    @Operation(summary = "取消借阅")
    @PostMapping("/cancel")
    public Result<?> cancelBorrow(@RequestParam Long userId, @RequestBody BorrowDTO borrowDTO) {
        BorrowRecord borrowRecord = borrowRecordService.cancelBorrow(userId, borrowDTO.getId());
        return Result.success(borrowRecord);
    }
    
    @Operation(summary = "查询用户借阅记录")
    @GetMapping("/user/records")
    public Result<?> getUserBorrowRecords(
            @RequestParam Long userId,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer size) {
        return Result.success(borrowRecordService.getUserBorrowRecords(userId, status, currentPage, size));
    }
    
    @Operation(summary = "查询借阅记录详情")
    @GetMapping("/{id}")
    public Result<?> getBorrowRecordById(@PathVariable Long id) {
        return Result.success(borrowRecordService.getBorrowRecordById(id));
    }
    
    @Operation(summary = "管理员查询所有借阅记录")
    @GetMapping("/admin/records")
    public Result<?> getAllBorrowRecords(
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) Long bookId,
            @RequestParam(required = false) String bookTitle,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer size) {
        return Result.success(borrowRecordService.getAllBorrowRecords(userId, bookId, bookTitle,status, currentPage, size));
    }
    
    @Operation(summary = "检查并更新逾期状态")
    @PostMapping("/check-overdue")
    public Result<?> checkAndUpdateOverdueStatus() {
        borrowRecordService.checkAndUpdateOverdueStatus();
        return Result.success();
    }

    @Operation(summary = "审批借阅申请")
    @PostMapping("/approve")
    public Result<?> approveBook(@RequestBody BorrowDTO borrowDTO) {
        BorrowRecord borrowRecord = borrowRecordService.approveBook(borrowDTO.getId());
        return Result.success(borrowRecord);
    }

    @Operation(summary = "拒绝借阅申请")
    @PostMapping("/reject")
    public Result<?> rejectBook(@RequestBody BorrowDTO borrowDTO) {
        BorrowRecord borrowRecord = borrowRecordService.rejectBook(borrowDTO.getId());
        return Result.success(borrowRecord);
    }
} 